#include <stdio.h>
#include <stdlib.h>

#define COUNT 30

struct deviceInfor {
  char devno[30];
  int devsort; // 1 代表微机  2代表打印机  3代表扫描机
  char devname[30];
  int devprice;
  char devbuydate[30];
  int devstate;
  char devdonedate[30];
} dev[COUNT];

void start(); //函数声明
void input();
void change();
void sort();
void dele();
void output();
void search();

int count = 0;

int main() //主函数
{
  while (1)
    start();
}

void start() //启动菜单
{
  int chi;
  printf("                            实验设备管理系统\n");
  printf("            |-----------------------------------------------|\n");
  printf("            |  ******************************************** |\n");
  printf("            |  1.设备信息的录入:                            |\n");
  printf("            |  2.设备信息的修改:                            |\n");
  printf("            |  3.设备信息的分类统计:                        |\n");
  printf("            |  4.删除某个设备:                              |\n");
  printf("            |  5.查找某个设备:                              |\n");
  printf("            |  6.退出系统:                                  |\n");
  printf("            |  ******************************************** |\n");
  printf("            |-----------------------------------------------|\n");
  printf("              输入你的选择: ");
  scanf("%d", &chi);
  // printf("\n");
  if (chi == 1)
    input();
  else if (chi == 2)
    change();
  else if (chi == 3)
    sort();
  else if (chi == 4)
    dele();
  else if (chi == 5)
    search();
  else if (chi == 6) {
    printf("你已经退出设备管理系统!\n");
    exit(0);
  } else {
    printf(" You Enter The Choice Is Not valid ! \n");
  }
}

void input() //数据录入
{

  char flag[20];
  do {
    printf("请输入你的设备信息:\n");
    printf("设备编号:");
    scanf("%s", dev[count].devno);
    printf("设备种类(1 代表微机  2代表打印机  3代表扫描机):");
    scanf("%d", &dev[count].devsort);
    printf("设备名字:");
    scanf("%s", dev[count].devname);
    printf("设备价格:");
    scanf("%d", &dev[count].devprice);
    printf("设备购买日期:");
    scanf("%s", dev[count].devbuydate);
    printf("设备状态(1未报废 0报废):");
    scanf("%d", &dev[count].devstate);
    printf("设备报废日期:");
    scanf("%s", dev[count].devdonedate);

    count++;
    printf("是否继续输入数据 y是  n否 : ");
    scanf("%s", flag);
  } while (strcmp(flag, "y") == 0 || strcmp(flag, "Y") == 0);

  output();
}

void change() //数据修改
{
  char ch[20], a[20];
  int i;
  printf("\nyou sure want change devInfor y/n): ");
  scanf("%s", ch);
  if (strcmp(ch, "y") == 0 || strcmp(ch, "Y") == 0) {
    printf("\nenter you want change devName:");
    scanf("%s", a);
    for (i = 0; i < count; i++) {
      if (strcmp(dev[i].devname, a) == 0) {
        printf("\nyou sure want change dev name(y/n): ");
        scanf("%s", ch);
        if (strcmp(ch, "y") == 0 || strcmp(ch, "Y") == 0) {
          printf("\nname:");
          scanf("%s", dev[i].devname);
        }
        printf("\nyou sure want change dev no(y/n): ");
        scanf("%s", ch);
        if (strcmp(ch, "y") == 0 || strcmp(ch, "Y") == 0) {
          printf("\nno");
          scanf("%s", dev[i].devno);
        }
        printf("\nyou sure want change dev sort(y/n): ");
        scanf("%s", ch);
        if (strcmp(ch, "y") == 0 || strcmp(ch, "Y") == 0) {
          printf("\nsort");
          scanf("%d", &dev[i].devsort);
        }
        printf("\nyou sure want dev price(y/n): ");
        scanf("%s", ch);
        if (strcmp(ch, "y") == 0 || strcmp(ch, "Y") == 0) {
          printf("\nprice");
          scanf("%d", &dev[i].devprice);
        }
        printf("\nyou sure want dev done date(y/n): ");
        scanf("%s", ch);
        if (strcmp(ch, "y") == 0 || strcmp(ch, "Y") == 0) {
          printf("\ndone date");
          scanf("%s", dev[i].devdonedate);
        }
      }
    }
  }

  output();
}
void sort() //数据分类显示
{
  int i, chi, sum = 0, wei = 0, da = 0, sao = 0;
  printf("输入你想要统计的信息: 1(报废的机器总数) 2(机器的总类数目)");
  scanf("%d", &chi);
  if (chi == 1) {
    for (i = 0; i < count; i++) {
      if (dev[i].devstate == 0)
        sum++;
    }
    printf("报废的机器数目为 %d \n", sum);
  } else if (chi == 2) {
    for (i = 0; i < count; i++) {
      if (dev[i].devsort == 1)
        wei++;
      else if (dev[i].devsort == 2)
        da++;
      else if (dev[i].devsort == 3)
        sao++;
    }
    printf("微机数目为 %d  打印机数目为 %d 扫描机数目为 %d", wei, da, sao);
  } else {
    printf("你输入的选项不符合要求!\n");
  }
}
void dele() //数据删除
{
  int i, j;
  char c[20];
  printf("\nenter you want delete name ：\n");
  printf("name：");
  scanf("%s", c);
  for (i = 0; i < count; i++)
    if (strcmp(c, dev[i].devname) == 0)
      break;
  for (j = i; j < count - 1; j++)
    dev[j] = dev[j + 1];
  printf("\t\t\tyou had delete %s\n", c);
  count--;
  output();
}
void output() //数据输出
{
  int i;
  for (i = 0; i < count; i++) {
    printf("%s %d %s %d %s %d %s\n", dev[i].devno, dev[i].devsort,
           dev[i].devname, dev[i].devprice, dev[i].devbuydate, dev[i].devstate,
           dev[i].devdonedate);
  }
}

void search() //数据查找
{
  int i;
  char a[20], ch[10];
  printf("\nenter you want look name:");
  scanf("%s", a);
  for (i = 0; i < count; i++)
    if (strcmp(dev[i].devname, a) == 0)
      printf("%s %d %s %d %s %d %s\n", dev[i].devno, dev[i].devsort,
             dev[i].devname, dev[i].devprice, dev[i].devbuydate,
             dev[i].devstate, dev[i].devdonedate);
}
